﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Baza.EF.DAL;
using Baza.EF.BLL;


namespace Pep_3014_3070.Owners
{
    public partial class OwnerApartmans : System.Web.UI.Page
    {
        protected int totalNumberPost
        {
            get { return (int)ViewState["totalPost"]; }
            set { ViewState["totalPost"] = value; }
        }
        protected int startPage
        {
            get { return (int)ViewState["StartPage"]; }
            set { ViewState["StartPage"] = value; }
        }
        protected int maxPage
        {
            get { return (int)ViewState["MaxPage"]; }
            set { ViewState["MaxPage"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                startPage = 0;
                maxPage = 10;
                totalNumberPost = DAOwners.getTotalCountApartmans(Convert.ToInt32(HttpContext.Current.User.Identity.Name));
                BindApartmans();
            }
        }

        private void BindApartmans()
        {
            List<ad_Owners_SELECT_All_By_OwnerID_Result> temp = DAOwners.getAllApartmansByOwnerID(Convert.ToInt32(HttpContext.Current.User.Identity.Name), startPage, maxPage, TitleInput.Text.Trim());

            if (temp.Count != 0)
            {
                ApartmansGridView.DataSource = temp;
                ApartmansGridView.DataBind();
                ApartmansGridView.HeaderRow.TableSection = TableRowSection.TableHeader;
                ApartmansGridView.UseAccessibleHeader = true;

            }
            else
            {
                ApartmansGridView.DataSource = temp;
                ApartmansGridView.DataBind();
            }
        }

        protected void PreviousButton_Click(object sender, EventArgs e)
        {
            if (startPage >= 0 && maxPage >= 0)
            {
                startPage -= 10;
                maxPage -= 10;
                if (startPage >= 0 && maxPage >= 0)
                    BindApartmans();
            }
        }

        protected void NextButton_Click(object sender, EventArgs e)
        {
            if (startPage >= 0 && maxPage <= totalNumberPost)
            {
                startPage += 10;
                maxPage += 10;
                if (startPage >= 0 && maxPage >= 0)
                    BindApartmans();
            }
        }

        protected void TitleInput_TextChanged(object sender, EventArgs e)
        {
            BindApartmans();
        }

        protected void AddNewPeriod_Click(object sender, EventArgs e)
        {
            BindApartmans();
        }

        protected void ApartmansGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "DeleteApartmanButton")
            {
                DAOwners.DeleteApartman_By_ApartmanID(Convert.ToInt32(e.CommandArgument));
                BindApartmans();
            }
        }
    }
}